package com.project.m.api.busi.handlers.hicatcard.user;

import com.alibaba.fastjson.JSON;
import com.project.m.api.busi.config.BusiError;
import com.project.m.api.busi.db.entity.hicatcard.user.TUser;
import com.project.m.api.busi.db.entity.hicatcard.user.TUserCard;
import com.project.m.api.busi.db.service.hicatcard.user.UserCardService;
import com.project.m.api.busi.db.service.hicatcard.user.UserService;
import com.project.m.api.busi.handlers.DefaultBizParamChecker;
import com.project.m.api.busi.bean.hicatcard.user.UserCardItem;
import com.project.m.api.busi.bean.hicatcard.user.UserCardListReq;
import com.project.m.api.busi.bean.hicatcard.user.UserCardListResp;
import com.project.m.api.busi.util.CommonConvertor;
import com.project.m.api.common.biz.BizHandler;
import com.project.m.api.validates.ValidateParam;
import com.project.m.api.validates.ValidateRet;
import com.project.m.api.validates.ValidateType;
import com.project.m.api.validates.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserCardListBizHandler extends DefaultBizParamChecker<UserCardListReq, UserCardListResp>
        implements BizHandler<UserCardListReq, UserCardListResp> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserService userService;

    @Autowired
    UserCardService userCardService;

    @Override
    public UserCardListResp handle(UserCardListReq bizRequest) throws Exception {
        UserCardListResp bizResp = new UserCardListResp();
        // 参数检查
        ValidateRet vRet = checkParam(bizRequest, bizResp);
        if (!vRet.isValid()) {
            return bizResp;
        }

        try {
            // 判斷用戶是否存在
            List<TUser> userList = userService.findByOpenidAndEnable(bizRequest.getOpenid(), true);
            if (userList == null || userList.size() == 0) {
                bizResp.setResult_code(BusiError.ERR_CODE_USER_NONE);
                bizResp.setError_message(BusiError.ERR_MSG_USER_NONE);
                return bizResp;
            }
            TUser user = userList.get(0);

            // 根据 source/ status 来取数据
            Sort.Order order = new Sort.Order(Sort.Direction.DESC, "createTime");
            Sort sort = new Sort(order);
            Pageable pageable = new PageRequest(bizRequest.getPage() - 1, bizRequest.getPagesize(), sort);
            List<UserCardItem> userCardItemList = userCardService.findUserCardItemList(user.getUserId(), bizRequest.getType(),
                    bizRequest.getSource(), bizRequest.getStatus(), bizRequest.getAttach(), true, pageable);

            List<UserCardItem> userCardItemResultList = new ArrayList<>();
            logger.info("userCardItemList = " + JSON.toJSONString(userCardItemList));
            for (int i = (bizRequest.getPage() - 1) * bizRequest.getPagesize(); i < bizRequest.getPage() * bizRequest.getPagesize(); i++) {
                if (i >= 0 && i < userCardItemList.size()) {
                    userCardItemResultList.add(userCardItemList.get(i));
                }
            }
//            int pageAdd = 1;
//            while (pageAdd < 10) {
//
//                logger.info("while (true) add = " + pageAdd);
//                if (userCardItemList.size() < bizRequest.getPagesize()) {
//                    pageable = new PageRequest((bizRequest.getPage() - 1) + pageAdd, bizRequest.getPagesize(), sort);
//                    List<UserCardItem> userCardItemAddList = userCardService.findUserCardItemList(user.getUserId(), bizRequest.getType(),
//                            bizRequest.getSource(), bizRequest.getStatus(), bizRequest.getAttach(), true, pageable);
//                    logger.info("userCardItemAddList.size() = " + userCardItemAddList.size());
//                    if (userCardItemAddList.size() == 0) {
//                        logger.info("userCardItemAddList.size()");
//                        break;
//                    }
//                    for (UserCardItem userCardItem : userCardItemAddList) {
//                        logger.info("userCardItemList.add");
//                        userCardItemList.add(userCardItem);
//                    }
//                }
//                else {
//                    break;
//                }
//                pageAdd++;
//            }
//
//            List<UserCardItem> userCardItemResultList = new ArrayList<>();
//            for (UserCardItem userCardItem : userCardItemList) {
//                userCardItemResultList.add(userCardItem);
//                if (userCardItemResultList.size() == bizRequest.getPagesize()) {
//                    break;
//                }
//            }


            bizResp.setResult_code(BusiError.ERR_CODE_SUCCESS);
            bizResp.setError_message("");
            bizResp.setDataList(userCardItemResultList);
            return bizResp;

        } catch (Exception e) {
            logger.error("UserCardListBizHandler", e);
            bizResp.setResult_code(BusiError.ERR_CODE_DB_SAVE);
            bizResp.setError_message(BusiError.ERR_MSG_DB_SAVE);
            return bizResp;
        }

    }

    @Override
    public ValidateRet checkParam(UserCardListReq req, UserCardListResp resp) {
        ValidateRet vRet = super.checkParam(req, resp);
        if (!vRet.isValid())
            return vRet;

        List<ValidateParam> paramList = new ArrayList<ValidateParam>();
        paramList.add(new ValidateParam("page", ValidateType.NOTBLANK, req.getPage()));
        paramList.add(new ValidateParam("page", ValidateType.INT, req.getPage()));
        paramList.add(new ValidateParam("pagesize", ValidateType.NOTBLANK, req.getPagesize()));
        paramList.add(new ValidateParam("pagesize", ValidateType.INT, req.getPagesize()));

        vRet = ValidateUtil.validate(paramList);

        if (vRet == null) {
            vRet = new ValidateRet(true, "");
        }

        req.setStatus(CommonConvertor.convertStringAvoidNull(req.getStatus()));
        if (!req.getStatus().isEmpty()) {
            if (!TUserCard.statusValidate(req.getStatus())) {
                vRet = new ValidateRet(false, "status " + BusiError.ERR_MSG_PARAM_BAD_REQ);
            }
        }

        req.setSource(CommonConvertor.convertStringAvoidNull(req.getSource()));
        if (!req.getSource().isEmpty()) {
            if (!TUserCard.sourceValidate(req.getSource())) {
                vRet = new ValidateRet(false, "source " + BusiError.ERR_MSG_PARAM_BAD_REQ);
            }
        }

        if (!vRet.isValid()) {
            resp.setResult_code(BusiError.ERR_CODE_PARAM_BAD_REQ);
            resp.setError_message(vRet.getErrMsg());
        }

        return vRet;
    }
}
